package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.powermaster.server.dao.Strategy2DeviceDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.Strategy2DeviceEntity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Strategy2DeviceImpl implements Strategy2DeviceDao {

    @Override
    public boolean insert(Strategy2DeviceEntity entity) {
        String sql = "replace into strategy_device(strategyID, deviceID) values(?,?)";
        Object[] kk = new Object[2];
        kk[0] = entity.getStrategyID();
        kk[1] = entity.getDeviceID();
        boolean updated = JdbcUtil.update(sql, kk);
        return updated;
    }

    @Override
    public boolean update(Strategy2DeviceEntity entity) {
        String sql = "update strategy_device set strategyID = ? where deviceID = ?";
        Object[] kk = new Object[2];
        kk[0] = entity.getStrategyID();
        kk[1] = entity.getDeviceID();
        boolean deleted = JdbcUtil.update(sql, kk);
        return deleted;
    }

    @Override
    public boolean delete(Object[] deviceList) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("delete from strategy_device where 1=1");
        if (deviceList != null && deviceList.length != 0) {
            int length = deviceList.length;
            buffer.append(" AND deviceID IN (");
            for (int i = 0; i < length; i++) {
                if (i != length - 1) {
                    buffer.append("?,");
                } else {
                    buffer.append("?)");
                }
            }
        } else {
            return true;
        }
        Object[] kk = deviceList;
        return JdbcUtil.update(buffer.toString(),kk);
    }

    @Override
    public boolean delete(int strategyID, Object[] deviceList) {
        StringBuffer buffer = new StringBuffer();
        List<Object> list = new ArrayList<>();
        buffer.append("delete from strategy_device where 1=1");
        buffer.append(" and strategyID = ?");
        list.add(strategyID);
        if (deviceList != null && deviceList.length != 0) {
            int length = deviceList.length;
            buffer.append(" and deviceID in (");
            for (int i = 0; i < length; i++) {
                if (i != length - 1) {
                    buffer.append("?,");
                } else {
                    buffer.append("?)");
                }
            }
        } else {
            return true;
        }
        list.addAll(Arrays.asList(deviceList));
        Object[] kk = list.toArray();
        return JdbcUtil.update(buffer.toString(),kk);
    }

    /**
     * 修改，strategyID下的所有和deviceID都需要删除
     * @param strategyID
     * @param deviceList
     * @return
     */
    @Override
    public boolean deleteStrategyIdOrInDevices(int strategyID, Object[] deviceList) {
        StringBuffer buffer = new StringBuffer();
        List<Object> list = new ArrayList<>();
        buffer.append("delete from strategy_device where ");
        buffer.append("  strategyID = ? or ");
        list.add(strategyID);
        if (deviceList != null && deviceList.length != 0) {
            int length = deviceList.length;
            buffer.append("  deviceID in (");
            for (int i = 0; i < length; i++) {
                if (i != length - 1) {
                    buffer.append("?,");
                } else {
                    buffer.append("?)");
                }
            }
        } else {
            return true;
        }
        list.addAll(Arrays.asList(deviceList));
        Object[] kk = list.toArray();
        return JdbcUtil.update(buffer.toString(),kk);
    }

    @Override
    public boolean deleteByStrategyIDs(Object[] ids) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("delete from strategy_device");
        if (ids != null && ids.length != 0) {
            int length = ids.length;
            buffer.append(" where strategyID in (");
            for (int i = 0; i < length; i++) {
                if (i != length - 1) {
                    buffer.append("?,");
                } else {
                    buffer.append("?)");
                }
            }
        } else {
            return true;
        }
        return JdbcUtil.update(buffer.toString(),ids);
    }

    @Override
    public boolean deleteByDeviceId(String deviceId) throws SQLException {
        String sql = "delete from strategy_device where 1 = ? and deviceID=?";
        Object[] kk = new Object[2];
        kk[0] = 1;
        kk[1] = deviceId;
        boolean deleted = JdbcUtil.update1(sql, kk);
        return deleted;
    }

    @Override
    public List<Strategy2DeviceEntity> findByStrategyID(Integer strategyID) {
        String sql = "select * from strategy_device where strategyID=?";
        Integer[] kk = new Integer[1];
        kk[0] = strategyID;
        List<Strategy2DeviceEntity> result = JdbcUtil.queryForList(sql, kk, Strategy2DeviceEntity.class);
        return result;
    }

    @Override
    public List<Strategy2DeviceEntity> findAll() {
        String sql = "select * from strategy_device where 1=1 ";
        List<Strategy2DeviceEntity> result = JdbcUtil.queryForList(sql, null, Strategy2DeviceEntity.class);
        return result;
    }

    @Override
    public Strategy2DeviceEntity findByDeviceId(String sn) {
        String sql = "select * from strategy_device where deviceID=?";
        String[] kk = new String[1];
        kk[0] = sn;
        Strategy2DeviceEntity result = JdbcUtil.queryForBean(sql, kk, Strategy2DeviceEntity.class);
        return result;
    }
}
